//
// Created by EmFi on 2023/5/12.
//

#ifndef DBMS_DATABASE_H
#define DBMS_DATABASE_H

#include <list>
#include <sstream>
#include <fstream>
#include <iostream>
#include "../Util/LogUtil.h"
#include "Point.h"

using namespace std;

class Database {
protected:
    struct Head {
        long block_size = sizeof(Point);
        long length = 0;
        long indexed = 0;
    };
    Head head;
    string fn;
    // internal methods
    list<Point> _select_all();
    Point _select(int index);
    virtual int _insert(Point point);
    virtual int _insert(list<Point> list);
    virtual void _clear();
    virtual Point _remove(int index);
    virtual Point _update(int index, Point point);
private:
    fstream conn;

    bool _open();
    bool _create();
    void _close();
    void _optimize();
    void _write_head();
    Point * _select_arr(int index, int length);
public:
    explicit Database(const string& fn);
    virtual ~Database();

    void printInfo() const;
    virtual void optimize();
    // length
    long count();
    // select
    Point select(int index);
    list<Point> selectAll();
    // insert
    virtual int insert(Point point);
    virtual int insert(list<Point> list);
    // remove
    virtual Point remove(int index);
    virtual void clear();
    // update
    virtual Point update(int index, Point point);
    // query
    virtual list<Point> query(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax, double wMin, double wMax);
};

#endif //DBMS_DATABASE_H
